package cc.rengu.igas.opms.core.service.trans.common;

import cc.rengu.igas.opms.common.enums.RespCodeEnum;
import cc.rengu.igas.opms.core.service.base.ManageService;
import cc.rengu.igas.opms.facade.bean.ImageAuthCodeResouce;
import cc.rengu.igas.opms.facade.request.GenImageAuthCodeRequest;
import cc.rengu.igas.opms.facade.response.GenImageAuthCodeResponse;
import cc.rengu.igas.share.core.model.GeetestResult;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.model.ImageCodeResult;
import cc.rengu.oltp.service.realize.ImageCodeService;
import cc.rengu.oltp.service.realize.impl.ImageCodeServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * 获取图形验证码服务
 *
 * @author keyi
 * @modify by wangcheng
 * @date 2020/3/23.
 */
public class GenImageAuthCodeService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        GenImageAuthCodeRequest genImageAuthCodeRequest = new GenImageAuthCodeRequest();
        ConvertUtil.convertOutput(genImageAuthCodeRequest);
        return genImageAuthCodeRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<GenImageAuthCodeResponse> bizResponse = new BizResponse<>();
        GenImageAuthCodeResponse genImageAuthCodeResponse = new GenImageAuthCodeResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);

        GenImageAuthCodeRequest genImageAuthCodeRequest = (GenImageAuthCodeRequest) request;
        String instId = genImageAuthCodeRequest.getHeader().getInstId();

        //生成图形验证码
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        ImageCodeService imageCodeService = new ImageCodeServiceImpl();
        ImageCodeResult imageCodeResult = imageCodeService.applyImageCode(instId, sysSeqNum, null);
        if (null == imageCodeResult || null == imageCodeResult.getResult()) {
            rglog.error("生成图片验证码失败!");
            throw new BizException(RespCodeEnum.GEN_CHECK_CODE_FAIL.getRespCode(), RespCodeEnum.GEN_CHECK_CODE_FAIL.getRespDesc());
        } else {
            genImageAuthCodeResponse.setImageAuthCodeSource(imageCodeResult.getImageType());
            genImageAuthCodeResponse.setImageAuthCodeSeqNum(sysSeqNum);
            if ("local".equals(imageCodeResult.getImageType())) {
                genImageAuthCodeResponse.setImageAuthCodeImage((String) imageCodeResult.getResult());
            } else if ("geetest".equals(imageCodeResult.getImageType())) {
                GeetestResult geetestResult = (GeetestResult) imageCodeResult.getResult();
                if (null != geetestResult) {
                    List<ImageAuthCodeResouce> imageAuthCodeResouceList = new ArrayList<>();
                    ImageAuthCodeResouce imageAuthCodeResouce = new ImageAuthCodeResouce();
                    Field[] fields = geetestResult.getClass().getDeclaredFields();
                    for (Field field : fields) {
                        field.setAccessible(true);
                        imageAuthCodeResouce.setResourceId(field.getName());
                        imageAuthCodeResouce.setResourceValue(field.get(geetestResult).toString());
                    }
                    imageAuthCodeResouceList.add(imageAuthCodeResouce);
                    genImageAuthCodeResponse.setImageAuthCodeResourceList(imageAuthCodeResouceList);
                }
            }
        }

        //设置应答
        bizResponse.setResult(genImageAuthCodeResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        GenImageAuthCodeResponse genImageAuthCodeResponse = (GenImageAuthCodeResponse) bizResponse.getResult();
        ConvertUtil.convertInput(genImageAuthCodeResponse);
    }
}
